package controller

import (
	"gin-epg/internal/app/common/rsp"
	"gin-epg/internal/app/common/util"
	"github.com/gin-gonic/gin"
)

func GetThirdPartToken(c *gin.Context) {
	// 获取get参数名为key对应的值
	key := c.Query("key")
	// 拼接 fileDownloadUrl 和 filePath
	addr := "localhost:3051"
	configAddr, err := util.GetConfigValue("leveldbRpcUrl")
	if err == nil {
		addr = configAddr.(string) // 进行类型断言
	}
	operator, err := util.NewThirdPartyTokenOperator(addr)
	if err != nil {
		rsp.Error(c, err.Error())
		return
	}
	value, err := operator.GetThirdPartyTokenValue(key)
	if err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.Success(c, "ok", value)
	}
}

func SaveThirdPartToken(c *gin.Context) {
	key := c.Query("key")
	value := c.Query("value")
	addr := "localhost:3051"
	configAddr, err := util.GetConfigValue("leveldbRpcUrl")
	if err == nil {
		addr = configAddr.(string) // 进行类型断言
	}
	operator, err := util.NewThirdPartyTokenOperator(addr)
	if err != nil {
		rsp.Error(c, err.Error())
		return
	}
	err = operator.StoreKeyValue(key, value)
	if err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.Success(c, "新增成功", value)
	}
}

func DeleteThirdPartToken(c *gin.Context) {
	// 获取get参数名为key对应的值
	key := c.Query("key")
	addr := "localhost:3051"
	configAddr, err := util.GetConfigValue("leveldbRpcUrl")
	if err == nil {
		addr = configAddr.(string) // 进行类型断言
	}
	operator, err := util.NewThirdPartyTokenOperator(addr)
	if err != nil {
		rsp.Error(c, err.Error())
		return
	}
	err = operator.DeleteThirdPartyToken(key)
	if err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.Success(c, "删除成功", nil)
	}
}
